package one.bugu.android.demon.ui.adapter.snatch;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.juefeng.android.framework.LKUtil;

import java.util.ArrayList;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.snatch.SnatchPeriodBean;
import one.bugu.android.demon.ui.widget.CustomListAdapter;
import one.bugu.android.demon.util.MyTextUtils;

/**
 * Description SnatchRecordAdapter 夺宝记录适配器
 *
 * @author liuxin
 * @date 2018/10/27 14:01
 */
public class SnatchRecordAdapter extends CustomListAdapter<SnatchPeriodBean.DataBean> {

    private Context mContext;

    public SnatchRecordAdapter(Context context, ArrayList<SnatchPeriodBean.DataBean> objList) {
        super(context, objList);
        this.mContext = context;
    }

    @Override
    protected View lpgetView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_snatch_record, null);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }

        if (mObjList != null) {
            setListData(mHolder, position);
        }
        return convertView;
    }

    /**
     * 设置列表数据
     *
     * @param mHolder
     * @param position
     */
    @SuppressLint("SetTextI18n")
    private void setListData(ViewHolder mHolder, int position) {
        SnatchPeriodBean.DataBean data = mObjList.get(position);
        if (data == null) {
            return;
        }
        //图片
        LKUtil.getImageLoader().loadImage(data.getGoodsImg(), mHolder.iv_items_image, R.mipmap.icon_snatch_def);
        //title
        mHolder.tv_item_title.setText("第" + data.getPeriodNo() + "期");
        //名称
        mHolder.tv_item_price.setText(data.getGoodsName());
        //订单编号
        mHolder.tv_item_order.setText(data.getOrderNo());
        //已购份数
        String text = "已抢购份数" + data.getBuyNum() + "份";
        SpannableString afterText = MyTextUtils.getInstance().setAutoTextColor(text, "[0-9]{1,}", "#5856D0");
        mHolder.tv_item_count.setText(afterText);
        //中奖 中奖状态 10:进行中 20:待揭晓 30:已中奖未分享 40:已中奖已分享 50:未中奖
        boolean isWin = data.getWinStatus() == 30 || data.getWinStatus() == 40;
        mHolder.iv_items_win.setVisibility(isWin ? View.VISIBLE : View.GONE);
        mHolder.ll_item_win_num.setVisibility(isWin ? View.VISIBLE : View.GONE);
        mHolder.tv_item_win_num.setText(data.getLuckyCode());

        String btnText = "查看详情";
        String btnTextColor = "#5856D0";
        switch (data.getWinStatus()) {
            case 10:
                btnText = "查看订单";
                btnTextColor = "#5856D0";
                break;
            case 20:
                btnText = "等待开奖";
                btnTextColor = "#5856D0";
                break;
            case 30:
            case 40:
                btnText = "已开奖";
                btnTextColor = "#999999";
                break;
            case 50:
                btnText = "未中奖";
                btnTextColor = "#999999";
                break;
        }
        mHolder.tv_bottom_btn.setText(btnText);
        mHolder.tv_bottom_btn.setTextColor(Color.parseColor(btnTextColor));
    }

    public static class ViewHolder {
        public View rootView;
        public TextView tv_item_title;
        public TextView tv_item_price;
        public TextView tv_item_order;
        public TextView tv_item_win_num;
        public LinearLayout ll_item_win_num;
        public TextView tv_item_count;
        private ImageView iv_items_image;
        private ImageView iv_items_win;
        private TextView tv_bottom_btn;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_item_title = rootView.findViewById(R.id.tv_item_title);
            this.tv_item_price = rootView.findViewById(R.id.tv_item_price);
            this.tv_item_order = rootView.findViewById(R.id.tv_item_order);
            this.tv_item_win_num = rootView.findViewById(R.id.tv_item_win_num);
            this.ll_item_win_num = rootView.findViewById(R.id.ll_item_win_num);
            this.tv_item_count = rootView.findViewById(R.id.tv_item_count);
            this.iv_items_image = rootView.findViewById(R.id.iv_items_image);
            this.iv_items_win = rootView.findViewById(R.id.iv_items_win);
            this.tv_bottom_btn = rootView.findViewById(R.id.tv_bottom_btn);
        }

    }
}
